//
//  main.m
//  05-Homework
//
//  Created by Snow on 16/7/8.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "ModelOfCity.h"
#import "ModelOfProvince.h"
#import "Location.h"

int main(int argc, const char * argv[]) {
    // 一、
     
     // 1.创建1个包含5个字符串元素的数组；
    NSArray *arrayOfString = @[@"one", @"two", @"three", @"four", @"five"];
     // 2.用for循环倒序输出所有元素；
    for (NSInteger i = 4; i >= 0; i--) {
        NSLog(@"倒叙输出:%@", arrayOfString[i]);
    }
     // 3.用for...in完成所有字符串元素的拼接(生成一个
     // 新的字符串)，并输出拼接好的字符串.
    NSString *newString = [[NSString alloc] init];
    for (NSString *string in arrayOfString) {
        newString = [newString stringByAppendingString:string];
    }
    NSLog(@"拼接好的字符串为:%@", newString);
    
    // 二、
     
     //1.创建1个包含5个person对象的数组(姓名用英文)；
    Person *per1 = [Person personWithName:@"Tom" age:18 gender:@"male"];
    Person *per2 = [Person personWithName:@"Lisa" age:19 gender:@"famale"];
    Person *per3 = [Person personWithName:@"Cherry" age:16 gender:@"famale"];
    Person *per4 = [Person personWithName:@"Cindy" age:17 gender:@"famele"];
    Person *per5 = [Person personWithName:@"Herry" age:20 gender:@"male"];
    NSArray *arrayOfPerson = @[per1, per2, per3, per4, per5];
     //2.用for循环找到年龄最大的Person对象；
    NSUInteger index = 0;
    for (NSInteger i = 0; i < arrayOfPerson.count; i++) {
        NSInteger max = 0;
        Person *per = arrayOfPerson[i];
        if (per.age > max) {
            index = i;
        }
    }
    Person *per = arrayOfPerson[index];
    NSLog(@"年龄最大的人是:%@,%ld岁", per.name, per.age);
     //3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    NSInteger age = 18;
    for (Person *per in arrayOfPerson) {
        if (per.age > age) {
            per.name = [per.name stringByAppendingString:@"-brother"];
        }
        NSLog(@"%@", per.name);
    }
    //三、
     
     //1.使用第二题目中生成的数组；
     //2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
    NSArray *arrOfName = [arrayOfPerson sortedArrayUsingSelector:@selector(compareByName:)];
    for (Person *per in arrOfName) {
        NSLog(@"按姓名:%@", per.name);
    }
    NSArray *arrOfGender = [arrayOfPerson sortedArrayUsingSelector:@selector(compareByGender:)];
    for (Person *per1 in arrOfGender) {
        NSLog(@"按性别:%@", per1.gender);
    }
    NSArray *arrOfAge = [arrayOfPerson sortedArrayUsingSelector:@selector(compareByAge:)];
    for (Person *per2 in arrOfAge) {
        NSLog(@"按年龄:%ld", per2.age);
    }
    //3.使用NSSortDescriptor，按姓名进行排序。
    NSSortDescriptor *sortOfName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];//姓名
    NSArray *perName = [arrayOfPerson sortedArrayUsingDescriptors:@[sortOfName]];
    NSLog(@"%@", perName);
     //四、
     
     //处理省市区文件中的area.txt
     //将省市区进行分割处理, 省里包含市, 市里包含区
     //1. 先用数组字典表示
#if 0
    NSString *path = @"/Users/Snow/DLS160710/Objective-C/HomeWork/05-Homework/05-Homework/area.txt";
    NSString *strSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *mainArray = [strSource componentsSeparatedByString:@"\n"];
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *string in mainArray) {
         if (![string hasPrefix:@" "])
         {
            NSMutableDictionary *dicOfProvince = [NSMutableDictionary dictionary];
            [dicOfProvince setValue:string forKey:@"provinceName"];
            [array addObject:dicOfProvince];
             NSMutableArray *arrOfCity = [NSMutableArray array];
             [dicOfProvince setValue:arrOfCity forKey:@"arrayOfCity"];
         }
        if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
            NSMutableDictionary *dicOfProvince = [array lastObject];
            NSMutableArray *arrayOfCity = [dicOfProvince objectForKey:@"arrayOfCity"];
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setValue:string forKey:@"cityName"];
            [arrayOfCity addObject:dicOfCity];
            NSMutableArray *arrOfArea = [NSMutableArray array];
            [dicOfCity setValue:arrOfArea forKey:@"arrayOfArea"];
        }
        if ([string hasPrefix:@"    "]) {
            NSMutableDictionary *dicOfProvince = [array lastObject];
            NSMutableArray *arrayOfCity = [dicOfProvince objectForKey:@"arrayOfCity"];
            NSMutableDictionary *dicOfCity = [arrayOfCity lastObject];
            NSMutableArray *arrayOfArea = [dicOfCity objectForKey:@"arrayOfArea"];
            [arrayOfArea addObject:string];
        }
    }
    for (NSDictionary *dic in array) {
        NSLog(@"%@", [dic objectForKey:@"provinceName"]);
        NSArray *arr = [dic objectForKey:@"arrayOfCity"];
        for (NSDictionary *dicOfCity in arr) {
            NSLog(@"%@", [dicOfCity objectForKey:@"cityName"]);
            NSArray *arr1 = [dicOfCity objectForKey:@"arrayOfArea"];
            for (NSString *str in arr1) {
                NSLog(@"%@", str);
            }
        }
    }

     //2. 将数组字典的处理方式转换成数组模型(Class)
    NSMutableArray *newArray = [NSMutableArray array];
    for (NSDictionary *dic1 in array) {
        ModelOfProvince *modelOfProvince = [[ModelOfProvince alloc] init];
        [modelOfProvince setValuesForKeysWithDictionary:dic1];
        for (NSString *key in dic1) {
            if ([key isEqualToString:@"provinceName"]) {
                [modelOfProvince setValue:[dic1 objectForKey:key] forKey:key];
            }
            if ([key isEqualToString:@"arrayOfCity"]) {
                NSArray *array = [dic1 objectForKey:key];
               NSMutableArray *array1 = [NSMutableArray arrayWithArray:array];
                [modelOfProvince setValue:array forKey:key];
                for (NSDictionary *dic3 in array) {
                    ModelOfCity *modelOfCity = [[ModelOfCity alloc] init];
                    [modelOfCity setValuesForKeysWithDictionary:dic3];
                    for (NSString *key1 in dic3) {
                    if ([key1 isEqualToString:@"cityName"]) {
                        [modelOfCity setValue:[dic3 objectForKey:key1] forKey:key1];
                    }
                    if ([key1 isEqualToString:@"arrayOfArea"]) {
                        [modelOfCity setValue:[dic3 objectForKey:key1] forKey:key1];
                    }
                    [array1 addObject:modelOfCity];
                    }
                }
            }
        }
        [newArray addObject:modelOfProvince];
    }
    NSLog(@"%@", newArray);

    
    //五、拓展
     //上述作业做完, 处理Location1.txt

    NSString *path2 = @"/Users/Snow/DLS160710/Objective-C/HomeWork/05-Homework/05-Homework/Location1.txt";
    NSString *mainString2 = [NSString stringWithContentsOfFile:path2 encoding:NSUTF8StringEncoding error:nil];
    NSMutableArray *arrOfLocation = [NSMutableArray array];
    NSArray *mainArray2 = [mainString2 componentsSeparatedByString:@","];
    for (NSString *str in mainArray2) {
        NSArray *arrLocation = [str componentsSeparatedByString:@"|"];
        Location *location = [[Location alloc] init];
        location.name = [arrLocation firstObject];
        location.number = [arrLocation lastObject];
        [arrOfLocation addObject:location];
    }
    NSMutableArray *arrPro = [NSMutableArray array];
    for (Location *location in arrOfLocation) {
        if ([[location.number substringFromIndex:location.number.length - 4] isEqualToString:@"0000"]) {
            NSMutableDictionary *locationDic = [NSMutableDictionary dictionary];
            [locationDic setValue:location.name forKey:@"provinceName"];
            [locationDic setValue:location.number forKey:@"provinceNumber"];
            [locationDic setValue:[location.number substringToIndex:3] forKey:@"pieceProvince"];
            NSMutableArray *cityArray = [NSMutableArray array];
            [locationDic setObject:cityArray forKey:@"cityArray"];
            [arrOfLocation addObject:locationDic];
        } else if ([[location.number substringFromIndex:location.number.length - 2] isEqualToString:@"00"] && [location.number characterAtIndex:location.number.length - 3] != '0') {
            for (NSMutableDictionary *provinceDic in arrPro) {
                if ([provinceDic[@"pieceProvince"] isEqualToString:[location.number substringToIndex:3]]) {
                    NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                    [cityDic setObject:location.name forKey:@"cityName"];
                    [cityDic setObject:location.number forKey:@"cityNumber"];
                    [cityDic setObject:[location.number substringToIndex:4] forKey:@"pieceCity"];
                    NSMutableArray *areaArray = [NSMutableArray array];
                    [cityDic setObject:areaArray forKey:@"areaArray"];
                    [provinceDic[@"cityArray"] addObject:cityDic];
                }
            }
        } else {
            for (NSMutableDictionary *provinceDic in arrPro) {
                if ([provinceDic[@"pieceProvince"] isEqualToString:[location.number substringToIndex:3]]) {
                    for (NSMutableDictionary *cityDic in provinceDic[@"cityArray"]) {
                        if ([cityDic[@"pieceCity"] isEqualToString:[location.number substringToIndex:4]]) {
                            NSMutableDictionary *districtDic = [NSMutableDictionary dictionary];
                            [districtDic setObject:location.name forKey:@"areaName"];
                            [districtDic setObject:location.number forKey:@"areaNumber"];
                            [cityDic[@"areaArray"] addObject:districtDic];
                        }
                    }
                }
            }
        }
        
    }
    NSLog(@"%@", mainArray2);
#endif
    
    //要求同第四题。自己寻找规律
     //六、如果都做完
     
     //额外找我要作业, 练习有的是, 就怕你不练
    
    return 0;
}
